﻿using System;
using System.ServiceModel.Channels;

namespace InterrogazioneWebServicesBPM
{
    class SecurityHeader : MessageHeader
    {
        private HeaderDataUser hdu;       

        public SecurityHeader():base(){
            this.hdu = new HeaderDataUser();//default;
        }
        
        public SecurityHeader(string userName, string pwdText):base() {
           this.hdu = new HeaderDataUser(userName,pwdText); 
        }
        protected override void OnWriteHeaderContents(System.Xml.XmlDictionaryWriter writer, MessageVersion messageVersion)
        {
            this.setUsernameTokenAndPassowrdText(writer, messageVersion);            
            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="messageVersion"></param>
        private void setUsernameTokenAndPassowrdText(System.Xml.XmlDictionaryWriter writer, MessageVersion messageVersion)
        {
            var timestamp = DateTime.Now;
            var secExtNamespace = WsSecurityConstants.WSS_SECURITY_SECEXT_1_0;

            writer.WriteStartElement(WsSecurityConstants.USERNAME_TOKEN_TAG, secExtNamespace);
            writer.WriteElementString(WsSecurityConstants.USERNAME_TAG, secExtNamespace, hdu.User);
            writer.WriteStartElement(WsSecurityConstants.PASSWORD_TAG);
            writer.WriteAttributeString(WsSecurityConstants.TYPE_ATTRIBUTE,
                WsSecurityConstants.PASSWORD_TEXT_BASE_PROFILE_XSD_1_0);
            writer.WriteString(hdu.Password);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }        

        public override string Name
        {
            get { return WsSecurityConstants.SECURITY_TAG; }
        }

        public override string Namespace
        {                
            get { return WsSecurityConstants.WSS_SECURITY_SECEXT_1_0; }
        }

        public override bool MustUnderstand
        {
            get { return false; }
        }
        public override string Actor
        {
            get { return WsSecurityConstants.ACTOR_ATTRIBUTE; }
        }

        
    }
}
